package com.xci.sys.api;

import com.xci.core.annotation.AllowAnonymous;
import com.xci.core.base.RestMessage;
import com.xci.core.internal.Const;
import com.xci.core.helper.ExcelHelper;
import com.xci.core.annotation.Authorize;
import com.xci.core.base.ApiController;
import com.xci.sys.entity.SysUser;
import com.xci.core.domain.OnlineUserModel;
import com.xci.sys.service.OnlineUserService;
import com.xci.sys.service.UserService;
import io.swagger.annotations.*;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * 在线用户接口
 * @author 吕艳阳
 */
@ApiSort(8)
@Api(tags = "在线用户接口")
@Authorize
@RestController
@RequestMapping(value = "/api/sys/onlineUser", produces = Const.P_JSON)
public class OnlineUserApiController extends ApiController {
    @Resource
    private OnlineUserService onlineUserService;//在线用户服务

    /**
     * 用户服务
     */
    @Resource
    private UserService userService;

    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "激活在线用户")
    @ApiImplicitParam(name = "userId", value = "用户主键", required = true)
    @PostMapping("/active")
    public RestMessage active(String userId) {
        SysUser user = userService.selectById(userId);
        if(user != null) {
            onlineUserService.active(user);
            return RestMessage.success();
        }
        return RestMessage.fail("激活失败,无效的用户主键");
    }

    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "注销在线用户")
    @ApiImplicitParam(name = "userId", value = "用户主键", required = true)
    @AllowAnonymous
    @PostMapping("/logoff")
    public RestMessage logoff(String userId) {
        onlineUserService.logoff(userId);
        return RestMessage.success();
    }

    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "根据账号注销在线用户")
    @ApiImplicitParam(name = "account", value = "用户账号", required = true)
    @AllowAnonymous
    @PostMapping("/logoffByAccount")
    public RestMessage logoffByAccount(String account) {
        onlineUserService.logoffByAccount(account);
        return RestMessage.success();
    }

    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "查询单个在线用户")
    @ApiImplicitParam(name = "userId", value = "用户主键", required = true)
    @PostMapping("/get")
    public RestMessage<OnlineUserModel> get(String userId) {
        return RestMessage.success(onlineUserService.get(userId));
    }

    @ApiOperationSupport(order = 4)
    @ApiOperation(value = "查询在线用户列表")
    @PostMapping("/query")
    public RestMessage<List<OnlineUserModel>> query() {
        return RestMessage.success(onlineUserService.query(null));
    }

    @ApiOperationSupport(order = 5)
    @ApiOperation(value = "导出在线用户列表")
    @Authorize(code = "sys.onlineUser.export")
    @PostMapping(value = "/export", produces = {Const.P_OCTET, Const.P_JSON})
    public void export() {
        ExcelHelper.exportWeb(onlineUserService.query(null), OnlineUserModel.class, "在线用户列表");
    }
}